/**
 *    Copyright 2009-2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.executor.statement;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.session.ResultHandler;

/**
 *                                 StatementHandler
 *                  ______________________|__________________________________________
 *                 |                                                                |
 *        BaseStatementHandler(抽象类)                                      RoutingStatementHandler
 *（封装了通用的处理逻辑及方法执行流程具体方法实现由子类完成，这里使用了设计模式中的模板方式模式）
 *     ___________|___________________________________________________
 *    |                                    |                        |
 * PreparedStatementHandler      SimpleStatementHandler      CallableStatementHandler
 *
 * 封装了对 JDBC Statement 的操作，例如设置 Statement 的 fetchSize 属性，设置查询超时时间，...
 * @author Clinton Begin
 */
public interface StatementHandler {

  /**
   * 该方法用于创建 JDBC Statement 对象，并完成Statement 对象的属性设置
   * @param connection
   * @return
   * @throws SQLException
   */
  Statement prepare(Connection connection)
      throws SQLException;

  /**
   * 设置 Statement 对象的参数
   * 该方法为Mybatis 中 ParameterHandler 组件PreparedStatement 和 CallableStatement参数占位符设置值
   * @param statement
   * @throws SQLException
   */
  void parameterize(Statement statement)
      throws SQLException;

  /**
   * 添加 Statement 对象的批量操作、
   * 将SQL命令添加到批处理执行列表中
   * @param statement
   * @throws SQLException
   */
  void batch(Statement statement)
      throws SQLException;

  /**
   * 执行更新操作
   * 调用 Statement 对象的 execute()方法执行更新语句，例如 UPDATE \ INSERT \ DELETE
   * @param statement
   * @return
   * @throws SQLException
   */
  int update(Statement statement)
      throws SQLException;

  /**
   * 执行读操作，
   * @param statement       Statement操作
   * @param resultHandler   ResultHandler 对象，处理结果
   * @param <E>
   * @return  读取的结果
   * @throws SQLException
   */
  <E> List<E> query(Statement statement, ResultHandler resultHandler)
      throws SQLException;

  BoundSql getBoundSql();

  /**
   * 获取 ParameterHandler 实例
   * @return
   */
  ParameterHandler getParameterHandler();

}
